package com.pig4cloud.pigx.knowledge.support.rule;

import com.pig4cloud.pigx.knowledge.dto.AiMessageResultDTO;
import com.pig4cloud.pigx.knowledge.dto.ChatMessageDTO;
import com.pig4cloud.pigx.knowledge.service.AiStreamAssistantService;
import com.pig4cloud.pigx.knowledge.support.provider.ModelProvider;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import reactor.core.publisher.Flux;

/**
 * 简单上下文聊天
 *
 * @author lengleng
 * @date 2024/3/26
 */
@Slf4j
@Component("simpleChat")
@RequiredArgsConstructor
public class SimpleChatRule implements ChatRule {

	private final ModelProvider modelProvider;

	public Flux<AiMessageResultDTO> process(ChatMessageDTO chatMessageDTO) {
		AiStreamAssistantService streamAssistantService = modelProvider
			.getAiStreamAssistant(chatMessageDTO.getModelName())
			.getValue();

		Flux<String> chatResponseFlux = streamAssistantService.chat(chatMessageDTO.getConversationId(),
				chatMessageDTO.getContent());
		// 转成 AiMessageResultDTO
		return chatResponseFlux.map(AiMessageResultDTO::new);

	}

}
